/*
 * Created on Oct 6, 2003
 * CountryStatsAction
 * Local Projects Database
 * (c) 2003 eRomania Gateway Association
 *
 */
package ro.gateway.aida.struts.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.stats.StatsDB;
import ro.gateway.aida.stats.StatsHolder;
import ro.gateway.aida.usr.Arbiter;
import ro.gateway.aida.usr.Perms;
import ro.gateway.aida.utils.BugTracker;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu
 * CountryStatsAction
 *
 *  *
 */
public class SourceOrgStatsAction extends TilesAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = getServlet().getServletContext();
            PersistenceToken token = PersistenceManager.tokenLookup(sc);
            String mode = request.getParameter("mode");
            StatsDB sdb = StatsDB.getManager(token);
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request,
                    sc);
            String lang = lang_bean.language;
            Arbiter ar = Arbiter.getManager(token);
            boolean show = false;

            if (ar.canDo(request, Perms.SHOW_UNAPPROVED))
                show = true;

            StatsHolder sh = sdb.getSourceOrgStats(lang,show);

            if (sh.size() != 0)
                request.setAttribute("cstats", sh);
            if ("widget".equals(mode)) {
                return mapping.findForward("stats.txt.srcorgs.widget");
            }

            return mapping.findForward("stats.txt.srcorgs.list");
        } catch (Exception e) {
            BugTracker.publishBug(e, request);
            return mapping.findForward("fail");
        }
    }
}
